﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.OpenGL;
using System.Drawing;
using Silvermoon.Animations;

using Silvermoon.Windows;
using System.Diagnostics;
using Silvermoon.Utils;
using Silvermoon.Controls.Base;
using Silvermoon.Images;
using Silvermoon.Shapes;
using Silvermoon.OpenGL.Native;
using Silvermoon.UI;

namespace Testing
{
    public class PivotDemo : DemoWindowBase
    {
        class MyPivotTile : NavigationNode, IBackNavigationNode
        {
            public Label Label { get; set; }
            public Label Title { get; set; }
            public Control Optional { get; set; }

            protected override void Dispose(bool dispose)
            {
                if (Label != null) Label.Dispose();
                Label = null;
                if (Title != null) Title.Dispose();
                Title = null;
                if (Optional != null) Optional.Dispose();
                Optional = null;
                base.Dispose(dispose);
            }
        }


        private Pivot titlePivot;
        private Pivot contentPivot;
        private MyPivotTile tile;
        private static FontSprite largeFont;
        private VisualStateManager manager;

        static PivotDemo()
        {

            largeFont = FontSprite.FromData(GlFontFamily.GenericSansSerif, 24, FontStyle.Bold, false);
        }

        public PivotDemo()
            : base()
        {

            SetTransition(500, TransitionMask.RotateTop | TransitionMask.Zoom | TransitionMask.Fade, TransitionMask.RotateBottom | TransitionMask.Zoom | TransitionMask.Fade);
            ToolbarColor = PhoneColors.DeepBlue;
            CreateManager();

            Controls.Add(new GradientControl
            {
                GradientColor = PhoneColors.Green,
                BottomColor = PhoneColors.DeepBlue
            });


            titlePivot = new Pivot
            {
                TileSizeFactor = 2f,
                Margin = new Margin(0, 0, 0, null),
                Height = 64,
                Power = 2d,
                //UnselectedTextColor = Color.Red,
                Mode = PivotMode.Scale | PivotMode.Transparency,
                Background = new GradientShape { EndColor = PhoneColors.FromArgb(255, 0, 0, 0), StartColor = PhoneColors.FromArgb(255, 48, 48, 48) }
            };

            contentPivot = new Pivot
            {
                TileSizeFactor = 1.3f,
                Margin = new Margin(0, 64, 0, 64),
                Power = 20d,
                Mode = PivotMode.Transparency
            };

            contentPivot.GetControl += (s, e) => e.Control = (e.Navigation as MyPivotTile).Label;
            titlePivot.GetControl += (s, e) => e.Control = (e.Navigation as MyPivotTile).Title;

            tile = CreateTiles();
            tile.Control.Width = 200;
            tile.Width = 200;
            titlePivot.SelectedNode = tile;
            if (contentPivot != null)
            {
                contentPivot.SelectedNode = tile;
            }

            Controls.Add(titlePivot);
            if (contentPivot != null) Controls.Add(contentPivot);

            Navigator navigator = new Navigator
            {
                Margin = new Margin(0, null, 0, 0),
                Height = 64,
                Background = new GradientShape { EndColor = PhoneColors.FromArgb(128, 0, 0, 0), StartColor = PhoneColors.FromArgb(128, 48, 48, 48) }
            };
            //navigator.SynchronizeWith(titlePivot, true);
            navigator.SelectedNode = tile;
            Controls.Add(navigator);
            navigator.SynchronizeWith(contentPivot);
            navigator.SynchronizeWith(titlePivot);
            titlePivot.SynchronizeWith(navigator);
            titlePivot.SynchronizeWith(contentPivot);


            CreateToolbarButtons();

        }

        private void CreateToolbarButtons()
        {
            ContentButton btn = new ToolbarButton();
            toolbar.Buttons.Add(btn);
            btn.Content = "5";
            btn.Tap += (s, e) =>
            {
                manager.GoTo(titlePivot, "5", true);
            };

            btn = new ToolbarButton();
            toolbar.Buttons.Add(btn);
            btn.Content = "3";
            btn.Tap += (s, e) =>
            {
                manager.GoTo(titlePivot, "3", true);
            };

            btn = new ToolbarButton();
            toolbar.Buttons.Add(btn);
            btn.Content = "1";
            btn.Tap += (s, e) =>
            {
                manager.GoTo(titlePivot, "1", true);
            };
        }

        private void CreateManager()
        {
            manager = new VisualStateManager(
                new VisualState("3",
                    new FloatTransition
                    {
                        Duration = 450,
                        EasingFunction = SineEase.InOut,
                        Property = Pivot.TileSizeFactorProperty,
                        Tracking = TrackMode.Keep,
                        To = 2f
                    }),
                new VisualState("5",
                    new FloatTransition
                    {
                        Duration = 450,
                        EasingFunction = SineEase.InOut,
                        Property = Pivot.TileSizeFactorProperty,
                        Tracking = TrackMode.Keep,
                        To = 2.8f
                    }),
                new VisualState("1",
                    new FloatTransition
                    {
                        Duration = 450,
                        EasingFunction = SineEase.InOut,
                        Property = Pivot.TileSizeFactorProperty,
                        Tracking = TrackMode.Keep,
                        To = 1f
                    })
                );
        }

        void pivot2_NodeChanged(object sender, EventArgs e)
        {
            titlePivot.SelectNode(contentPivot.SelectedNode, true);
        }

        void pivot_NodeChanged(object sender, EventArgs e)
        {
            contentPivot.SelectNode(titlePivot.SelectedNode, true);
        }

        void pivot2_EndScroll(object sender, EventArgs e)
        {
            Debug.WriteLine("EndScroll");
        }

        private MyPivotTile CreateTiles()
        {
            MyPivotTile tile = CreateTile("Root Item");
            tile.TextColor = Color.SkyBlue;
            //(tile.Control as Label).Font = FontSprite.Bold;
            tile.Optional = new Button { Content = "-", Color = Color.FromArgb(48, 0, 0), Margin = new Margin(null, 4, 4, 4), Width = 64 };
            tile.Width = 300;
            MyPivotTile t = tile;
            for (int i = 1; i < 10; i++)
            {
                MyPivotTile next = CreateTile(i.ToString() + "Positive");
                t.Next = next;
                t = next;
            }

            t = tile;
            for (int i = 1; i < 5; i++)
            {
                MyPivotTile prev = CreateTile((-i).ToString() + " Negative");
                t.Previous = prev;
                t = prev;
            }
            var ext = tile.Next as MyPivotTile;
            ext.Optional = new Button { Color = Color.FromArgb(0, 48, 0), Content = "+", Margin = new Margin(null, 4, 4, 4), Width = 64 };
            return tile;
        }

        int bs = 200;

        private MyPivotTile CreateTile(string title)
        {
            MyPivotTile tile = new MyPivotTile();
            Label label2 = new Label
            {
                Width = 120,
                Margin = Margin.Zero,
                Alignment = Alignment.Center,
                LineAlignment = Alignment.Center,
                Text = title,
                Font = FontSprite.RegularShadowed,
                LineMode = LineMode.SingleLine
            };
            //Button label2 = new Button { Margin = Margin.Zero, Text = title };
            tile.Control = label2;
            tile.Size = new Size(bs, 100);
            bs = 250 - bs;
           // tile.TextColor = Color.White;
            Label label = new Label
            {
                Width = 220,
                Margin = Margin.Zero,
                Alignment = Alignment.Center,
                LineAlignment = Alignment.Center,
                Text = title,
                IsShadowed = true,
                Font = largeFont
            };
            tile.Label = label;
            label = new Label
            {
                Width = 190,
                Margin = Margin.Zero,
                Alignment = Alignment.Center,
                LineAlignment = Alignment.Center,
                Text = title,
                LineMode = LineMode.SingleLine,
                Font = FontSprite.BoldShadowed
            };
            //label = title;
            tile.Title = label;
            return tile;
        }
    }
}
